package com.cjjd.c.http;

import android.text.TextUtils;

import com.cjjd.c.utils.LogUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class JsonUtils {


    /**
     * Json 转成 Map<>
     *
     * @param jsonStr
     * @return
     */
    public static Map<String, Object> getMapForJson(String jsonStr) {
        JSONObject jsonObject;
        try {
            jsonObject = new JSONObject(jsonStr);

            Iterator<String> keyIter = jsonObject.keys();
            String key;
            Object value;
            Map<String, Object> valueMap = new HashMap();
            while (keyIter.hasNext()) {
                key = keyIter.next();
                value = jsonObject.get(key);
                valueMap.put(key, value);
            }
            return valueMap;
        } catch (Exception e) {
            e.printStackTrace();
            // Log.e(HttpClientUtils.TAG, e.toString());
        }
        return null;
    }

    /**
     * Json 转成 List<Map<>>
     *
     * @param jsonStr
     * @return
     */
    public static List<Map<String, Object>> getlistForJson(String jsonStr) {
        List<Map<String, Object>> list = null;
        try {
            JSONArray jsonArray = new JSONArray(jsonStr);
            JSONObject jsonObj;
            list = new ArrayList<Map<String, Object>>();
            for (int i = 0; i < jsonArray.length(); i++) {
                jsonObj = (JSONObject) jsonArray.get(i);
                list.add(getMapForJson(jsonObj.toString()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * jsonString转换成list
     */
    public static List<Serializable> stringJsonToList(String json) {
        try {
            Type type = new TypeToken<ArrayList<Serializable>>() {
            }.getType();
            Gson gson = new Gson();
            List<Serializable> list = new ArrayList<Serializable>();
            if (!TextUtils.isEmpty(json)) {
                list = gson.fromJson(json, type);
                if (list != null && list.size() > 0) {
                    return list;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }


    /**
     * map to json
     *
     * @param mParams
     * @return
     */
    public static JSONObject convertHashMapToJson(HashMap<String, Object> mParams) {

        JSONObject jsObj = new JSONObject();
        if (mParams != null && mParams.size() >= 0) {
            Set<String> keys = mParams.keySet();
            for (String key : keys) {
                try {
                    if (mParams.get(key) instanceof HashMap) {
                        jsObj.put(key, convertHashMapToJson((HashMap<String, Object>) mParams.get(key)));
                    } else {
                        jsObj.put(key, mParams.get(key));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            LogUtil.e("xyd", "convertHashMapToJson : " + jsObj.toString());
        }
        return jsObj;
    }


    /**
     * 生成请求参数
     *
     * @param openType
     * @param hashMap
     * @return
     */
    public static String convertParam(String openType, HashMap<String, Object> hashMap) {

        StringBuffer paramString = new StringBuffer();
        paramString.append("{\"");
        paramString.append("opeType");
        paramString.append("\"");
        paramString.append(":");
        paramString.append("\"");
        paramString.append(openType);
        paramString.append("\"");
        paramString.append(",");

        paramString.append("\"map\":");

        paramString.append(convertHashMapToJson(hashMap));

        paramString.append("}");

        LogUtil.e("xyd", "convertParam : " + paramString);
        return paramString.toString();
    }


    public static String convertListParam(List<String> list) {

        StringBuffer paramString = new StringBuffer();
        paramString.append("[");

        for (String s:list){
            paramString.append("\"");
            paramString.append(s);
            paramString.append("\"");
            paramString.append(",");
        }
        paramString.deleteCharAt(paramString.length()-1);
        paramString.append("]");
        return paramString.toString();
    }

}
